package com.example.booksshop.controller;
import com.example.booksshop.entity.User;
import com.example.booksshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author xuan 2021/9/14
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "login", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public String login(@RequestParam String username,@RequestParam String password) {
        String result = userService.userExsit(username, password);
        return result;
    }

    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String insertUser(@RequestBody User user) {
        userService.insert(user);
        return "success";
    }

    @RequestMapping(value = "/select", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public User selectUser(@RequestParam int id) {
        return userService.select(id);
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String deleteUser(@RequestParam Integer id) {
        userService.delete(id);
        return "success";
    }

    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String userUpdate(@RequestBody User user) {
        int result = userService.update(user);

        if (result > 0) {
            return "success";
        }
            return "failed";
    }

    @RequestMapping(value = "/show", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<User> showUser() {
        return userService.show();
    }
}
